package com.java.servlet.web.mail.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.java.servlet.web.conexion.PoolConnection;
import com.java.servlet.web.mail.dao.sql.SqlMail;
import com.java.servlet.web.mail.vo.MailPropertiesVo;

public class MailDaoImpl implements MailDao {
	private static final Logger log = Logger.getLogger(MailDaoImpl.class);
	private Connection conn = null;

	public MailDaoImpl() {
		conn = PoolConnection.getInstance().getConection();
	}
	@Override
	public List<MailPropertiesVo> getListProperties() {
		PreparedStatement stm = null;
		ResultSet rs = null;
		List<MailPropertiesVo> listaProperties = null;
		try {
				stm = conn.prepareStatement(SqlMail.DATOS_MAIL);
				log.info("DATOS_MAIL  [ " + SqlMail.DATOS_MAIL);
			rs = stm.executeQuery();
			listaProperties = getListaPropsResultSet(rs);
		} catch (SQLException ex) {
			log.error("getListProperties  [ " + ex);
			PoolConnection.getInstance().closeConnection(conn);
		} finally {
			 try {
	                rs.close();
	                stm.close();
	            } catch (SQLException e) {
	            	log.error("SQLException  [ " + e);
	            } 
		}	
		return listaProperties;
	}
	private List<MailPropertiesVo> getListaPropsResultSet(ResultSet rs) {
		List<MailPropertiesVo> listaProps = null;
		try{
			if(rs.next()){
				listaProps = new ArrayList<MailPropertiesVo>();
				do{
					MailPropertiesVo mailPropertiesVo = new MailPropertiesVo();
					mailPropertiesVo.setDescProp(rs.getString("DESCRIPCION_PORP"));
					mailPropertiesVo.setValorProp(rs.getString("VALOR_POP"));

				    listaProps.add(mailPropertiesVo);
				}while(rs.next());
			}
		}catch(SQLException ex){
			log.error("getListaPropsResultSet   [  " + ex);
		}
		return listaProps;
	}

}

